package com.ttt.ejemplo.teststreaming2;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.IBinder;
import android.widget.Toast;

public class MusicService extends Service implements OnCompletionListener,
		OnPreparedListener, OnErrorListener /*
											 * 
											 * , , , OnSeekCompleteListener ,
											 * OnInfoListener ,
											 * OnBufferingUpdateListener
											 */{

	private MediaPlayer mediaPlayer = new MediaPlayer();

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	// Oncreate
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		mediaPlayer.setOnCompletionListener(this);
		mediaPlayer.setOnPreparedListener(this);
		mediaPlayer.setOnErrorListener(this);
		// mediaPlayer.setOnBufferingUpdateListener(this);
		// mediaPlayer.setOnSeekCompleteListener(this);
		// mediaPlayer.setOnInfoListener(this);
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		mediaPlayer.reset();

	}

	// Start service
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		mediaPlayer.reset();
		if (!mediaPlayer.isPlaying()) {
			try {
				mediaPlayer
						//.setDataSource("http://www.glowingpigs.com/audioclip/10.mp3");
						.setDataSource("http://www.platino1043.com.ar/PlatinoFM.m3u");

				// Prepare media player
				mediaPlayer.prepareAsync();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return START_STICKY;
	}

	// Stop Service
	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mediaPlayer != null) {
			if (mediaPlayer.isPlaying())
				mediaPlayer.stop();
			mediaPlayer.release();
		}
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		playMedia();
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
		stopMedia();
		stopSelf();
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		switch (what) {
		case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
			Toast.makeText(this,
					"MEDIA ERROR NOT VALID FOR PROGRESSIVE PLAYBACK " + extra,
					Toast.LENGTH_LONG).show();
			break;
		case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
			Toast.makeText(this, "MEDIA ERROR SERVER DIED " + extra,
					Toast.LENGTH_LONG).show();
			break;
		case MediaPlayer.MEDIA_ERROR_UNKNOWN:
			Toast.makeText(this, "MEDIA ERROR UNKNOWN " + extra,
					Toast.LENGTH_LONG).show();
			break;
		}
		return false;
	}

	// Start media player
	private void playMedia() {
		if (!mediaPlayer.isPlaying()) {
			mediaPlayer.start();
		}
	}

	// Stop media player
	private void stopMedia() {
		if (mediaPlayer.isPlaying()) {
			mediaPlayer.stop();
		}
	}

}